package com.emergency.db.service.impl.item;

import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.db.dao.item.HugeItemStatusDao;
import com.emergency.db.domain.item.HugeItemStatus;
import com.emergency.db.domain.staff.StaffStatus;
import com.emergency.db.service.impl.staff.StaffStatusServiceImpl;
import com.emergency.db.service.item.HugeItemStatusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class HugeItemStatusServiceImpl implements HugeItemStatusService {
    private final static Logger logger = LoggerFactory.getLogger(HugeItemStatusServiceImpl.class);

    @Autowired
    HugeItemStatusDao hugeItemStatusDao;
    @Autowired
    private IdWorker idWorker;

    //保存
    public Long save(HugeItemStatus hugeItemStatus) {
        if (CommonUtils.isNullOrEmpty(hugeItemStatus.getId())) {
            hugeItemStatus.setId(idWorker.nextId());
            hugeItemStatus.setCreateTime(new Date());
        }
        hugeItemStatusDao.save(hugeItemStatus);
        return hugeItemStatus.getId();
    }

    //查询单项
    public HugeItemStatus findById(Long id) {
        return hugeItemStatusDao.findById(id).get();
    }

    //查询单项ByItem
    public List<HugeItemStatus> findAllByItem(Long id) {
        List<HugeItemStatus> hugeItemStatusList = hugeItemStatusDao.findAllByItem(id);
        if (CommonUtils.isNullOrEmpty(hugeItemStatusList)) {
            logger.info("此人状态信息为空");
            return hugeItemStatusList;
        }
        return hugeItemStatusList;
    }

    //删除
    public void delete(Long id) {
        hugeItemStatusDao.deleteById(id);
    }

    //删除ByItem
    public void deleteAllByItem(Long id) {
        hugeItemStatusDao.deleteAllByItem(id);
    }

    //分页查询
    @Override
    public Page<HugeItemStatus> findList(Map searchMap, int page, int size) {
        Specification<HugeItemStatus> specification = createSpecification(searchMap);
        Sort sort = Sort.by(Sort.Direction.ASC, "createTime");
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        return hugeItemStatusDao.findAll(specification, pageRequest);
    }

    /**
     * 动态条件构建
     * @param searchMap
     * @return
     */
    private Specification<HugeItemStatus> createSpecification(Map searchMap) {
        return new Specification<HugeItemStatus>() {

            @Override
            public Predicate toPredicate(Root<HugeItemStatus> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<Predicate>();

                // 名称
                if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                    predicateList.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + (String) searchMap.get("name") + "%"));
                }
                // 已经工作时间
                if (searchMap.get(" workTime") != null && !"".equals(searchMap.get(" workTime"))) {
                    predicateList.add(criteriaBuilder.equal(root.get(" workTime").as(Long.class), (Long) searchMap.get(" workTime")));
                }
                // 装备使用状态
                if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                    predicateList.add(criteriaBuilder.equal(root.get("status").as(String.class), (String) searchMap.get("status")));
                }
                // 辐射剂量
                if (searchMap.get("dose") != null && !"".equals(searchMap.get("dose"))) {
                    predicateList.add(criteriaBuilder.ge(root.get("dose").as(double.class), (double) searchMap.get("dose")));
                }
                // 是否车辆
                if (searchMap.get("isCar") != null && !"".equals(searchMap.get("isCar"))) {
                    predicateList.add(criteriaBuilder.equal(root.get("isCar").as(String.class), (String) searchMap.get("isCar")));
                }

                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}
